package com.jdplat.jdm.web.controller.system;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.jdplat.jdm.core.web.controller.BaseController;
import com.jdplat.jdm.core.web.utils.AddrUtils;
import com.jdplat.jdm.core.web.utils.StrKit;
import com.jdplat.jdm.web.service.system.ISystemLogService;
import com.jdplat.jdm.web.service.system.ISystemPermissionService;
@Controller
@RequestMapping("/system")
public class SystemIndexController extends BaseController{
	
	@Autowired
	private ISystemLogService systemLogService;
//	@Autowired
//	private IBaiduService baiduService;
	@Autowired
	private ISystemPermissionService permissionService;
	
	/**
	 * 跳转到首页
	 */
	@RequiresAuthentication
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public ModelAndView login() {
		// 缓存菜单信息
		List<Map<String, Object>> data = null;
		if(existsCatch("leftMenu")) {
			String catchData = getCatch("leftMenu");
			data = toGson(catchData);
		} else {
			data = permissionService.permissionList(getCurrentUser().getId().toString());
			String catchData = toJson(data);
			putCatch("leftMenu", catchData , 60 * 3); // 3分钟后重置数据
		}
		addObject("data", data);
		return getSuccess("index/index");
	}
	/**
	 * 跳转到欢迎页
	 */
	@RequiresAuthentication
	@RequestMapping(value = "/welcome", method = RequestMethod.GET)
	public ModelAndView welcome() {
		Map<String, Object> view = Maps.newHashMap();

		List<Map<String, Object>> data = null;

		if (existsCatch("welcome")) {
			String catchData = getCatch("welcome");
			data = toGson(catchData);
		} else {
			data = systemLogService.select();
			String catchData = toJson(data);
			putCatch("welcome", catchData , 60 * 3); // 3分钟后重置数据
		}
		view.put("data", data);
		view.put("count", data.size());
		view.put("ip", "");
		view.put("last_time", "");

		if (data != null) {
			view.put("ip", data.get(0).get("IP"));
			view.put("last_time", data.get(0).get("tim"));
		}

		view.put("refCount", 0);
		//加入天气
		final String ipAddr = getIPaddr();
		
		/**
		 * 可以根据ip地址反查经纬度信息，地区信息，等等
		 */
//		if (!ipAddr.equals("127.0.0.1")) {
		if (true) {
			String weatherLog = getCatch(ipAddr);
			JSONObject weatherInfo = JSONObject.parseObject(weatherLog);
			
			view.put("refCount", "");
			
			String cityName = "";
			JSONObject weatherData = null;
			if(null != weatherInfo){
				cityName = weatherInfo.getString("cityName");
			    weatherData = JSONObject.parseObject(getCatch(ipAddr));
			}else{
				//cityName = AddrUtils.getCity(ipAddr);
				weatherData = getWeather("北京");
			}
			
			putCatch(ipAddr,weatherData.toString(),864000000);//缓存天气信息，以ip作为key,过期时间为10天
			
			view.put("cityName", cityName);
			view.put("temperature", weatherData.getString("temperature")); // 温度
			view.put("weather", weatherData.getString("weather")); // 天气状况
			view.put("wind", weatherData.getString("wind")); // 天气状况
			
			if (parseInt(StrKit.getHour()) > 18 ) {
				view.put("pictureUrl", weatherData.getString("nightPictureUrl"));
			} else {
				view.put("pictureUrl", weatherData.getString("dayPictureUrl"));
			}
		}
		addAllObjects(view);
		return getSuccess("index/welcome");
	}

}
